﻿// =====================================================================
// <copyright file="FileHelper.cs" author="David R. Guindo">
// 	Copyright (C) 2013. All rights reserved.
// </copyright>
// =====================================================================
// This code is released under the terms of the Apache 2.0 license, 
// http://apache.org/licenses/LICENSE-2.0.html
// =====================================================================

namespace AppBlock.Core
{
    using System.IO;
    using System.Reflection;
    using System.Web;

    public static class FileHelper
    {
        public static string GetEntirePath(string subpath = "")
        {
            string fullpath;

            /* Obtain the full filename of the configuration file. */
            if (HttpContext.Current != null)
            {
                fullpath = Path.Combine(HttpContext.Current.Server.MapPath("~"), subpath);
            }
            else
            {
                string path = System.AppDomain.CurrentDomain.BaseDirectory;
                //System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)

                if (path.StartsWith("file:\\"))
                {
                    path = path.Substring(6);
                }

                fullpath = Path.Combine(path, subpath);
            }

            return fullpath;
        }

        /// <summary>
        ///     Gets the full path.
        /// </summary>
        /// <param name="scriptFile"> The script file. </param>
        /// <returns> The full path from relative path </returns>
        public static string GetFullPath(string scriptFile)
        {
            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            if (path != null && path.StartsWith("file:\\"))
            {
                path = path.Substring(6);
                string filename = Path.Combine(path, scriptFile);
                return filename;
            }

            return null;
        }

        public static string GetRelativePath(string path, string basepath, int indentLevel = 0)
        {
            int level = path.Split('\\').Length - basepath.Split('\\').Length;
            string relpath = new string('*', level - indentLevel).Replace("*", "..\\");
            return relpath;
        }

        /// <summary>
        ///     Removes the read only.
        /// </summary>
        /// <param name="file"> The file. </param>
        public static void RemoveReadOnly(string file)
        {
            FileAttributes attributes = File.GetAttributes(file);
            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                File.SetAttributes(file, attributes ^ FileAttributes.ReadOnly);
            }
        }

        public static string GetRelativeTextPath(string file, string relative)
        {
            return file.Substring(relative.Length);
        }

        public static string GetLastDir(string path)
        {
            int pos = path.LastIndexOf("\\", System.StringComparison.Ordinal);
            if (pos < 0) return "";
            return path.Substring(pos + 1);
        }

        public static void XCopy(string folder, string targetFolder)
        {
            //if (File.Exists(
        }
    }
}